package com.mk.legoscans2.activities;

import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.mk.legoscans2.LegoScans3Application;
import com.mk.legoscans2.R;
import com.mk.legoscans2.Scan;
import com.mk.legoscans2.Utils;

public class ThemeListFragment extends ListFragment{

	private List<String> _themes;
	private ThemeListFragment _themeListFragment;
	
	private DownloadThemesTask _downloadThemesTask;
	private DownloadScansTask _downloadScansTask;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if(!Utils.isNetworkAvailable(getActivity()))
		{
			showNetworkErrorMessageAndEndActivity();
			return;
		}
		
		_themeListFragment = this;
		
		_downloadThemesTask = new DownloadThemesTask();
		_downloadThemesTask.execute();
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		
		String theme = _themes.get(position);
		
		ScanListFragment fragment = (ScanListFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.scans_fragment_container);
		if(fragment != null && fragment.isInLayout()) {
			_downloadScansTask = new DownloadScansTask(fragment, theme);
			_downloadScansTask.execute();
			
		} else {
			Intent scansIntent = new Intent(getActivity(), ScanListActivity.class);
			scansIntent.putExtra("theme", theme);
			startActivity(scansIntent);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.browse_by_theme_list, null);
	}
	
	@Override
	public void onDestroyView() {
		if(_downloadThemesTask != null)
		{
			_downloadThemesTask.cancel(true);
			_downloadThemesTask = null;
		}
		
		if(_downloadScansTask != null)
		{
			_downloadScansTask.cancel(true);
			_downloadScansTask = null;
		}
		
		super.onDestroyView();
	}
	
	private void showNetworkErrorMessageAndEndActivity()
	{
		Toast.makeText(getActivity(), getResources().getString(R.string.generic_network_error_message), Toast.LENGTH_SHORT).show();
		getActivity().finish();
	}
	
	/* DOWNLOAD THEMES TASK */
	private class DownloadThemesTask extends AsyncTask<String, Void, List<String>> {
		
		protected List<String> doInBackground(String... params) {
			return LegoScans3Application.ApiRepository.getThemes();
		}
		
		protected void onPostExecute(List<String> result) {
			_themes = result;
			if(_themes != null)
			{
				_themeListFragment.getActivity().findViewById(R.id.themeListProgressBar).setVisibility(View.GONE);
				_themeListFragment.getListView().setVisibility(View.VISIBLE);
				setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, _themes));
			}
			else
				showNetworkErrorMessageAndEndActivity();
		}
	}
	
	/* DOWNLOAD SCANS TASK */
	private class DownloadScansTask extends AsyncTask<String, Void, List<Scan>> {
		
		private ScanListFragment _scanListFragment;
		private String _theme;
		
		public DownloadScansTask(ScanListFragment fragment, String theme){
			_scanListFragment = fragment;
			_theme = theme;
		}
		
		protected List<Scan> doInBackground(String... params) {
			return LegoScans3Application.ApiRepository.getScansByTheme(_theme);
		}
		
		@Override
		protected void onPreExecute() {
			_scanListFragment.setIsLoading(true);
		}
		
		protected void onPostExecute(List<Scan> result) {
			LegoScans3Application.TempScans = result;
			_scanListFragment.updateList();
		}
	}
}
